<?php

namespace App\Admin\Actions\Audit;

use App\Library\Logic\OperateLogLogic;
use App\Models\AuctionCertificationModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class AuditAuctionCertificationAction extends RowAction
{
    public $name = '审核';

    public function handle(AuctionCertificationModel $model, Request $request): Response
    {
        if ((int)$model->status !== 0) return $this->response()->error('不可重复审核')->refresh();

        $model->status = $request['status'];
        if ((int)$request->status === 1) $model->ok_reason = is_null($request->reason) ? '' : $request->reason;
        else $model->fail_reason = is_null($request->reason) ? '' : $request->reason;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.auctionCertification.audit');
        return $this->response()->success('操作成功')->refresh();
    }

    public function form(AuctionCertificationModel $model): void
    {
        $this->select('status', '审核')->options([1 => '同意', 2 => '拒绝',])
            ->rules('required', ['required' => '请选择审核意见']);
        $this->textarea('reason', '原因')->rules('nullable|string|max:255');
    }
}
